package com.imooc.springcloud;

import com.imooc.springcloud.hystrix.RequestCacheService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import lombok.Cleanup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by 半仙.
 */
@RestController
public class Controller {

    @Resource
    private MyService myService;

    @Resource
    private RequestCacheService requestCacheService;

    @GetMapping("/fallback")
    public String fallback() {
        return myService.error();
    }

    @GetMapping("/timeout")
    public String timeout(Integer timeout) {
        return myService.retry(timeout);
    }

    @GetMapping("/cache")
    public Friend cache(String name) {
        // 声明上下文 保证只调用一次

//        HystrixRequestContext context = HystrixRequestContext.initializeContext();
//        try {
//            Friend friend = requestCacheService.requestCache(name);
//            friend = requestCacheService.requestCache(name);
//            return friend;
//        } finally {
//            context.close();
//        }
        // lombok 中的@Cleanup 相当于 自动调用context的close方法
        @Cleanup HystrixRequestContext context = HystrixRequestContext.initializeContext();
        Friend friend = requestCacheService.requestCache(name);
        friend = requestCacheService.requestCache(name);
        return friend;


    }

    /**
     * 针对某个方法自定义设置超时
     * @param timeout
     * @return
     */
    @GetMapping("/timeout2")
    @HystrixCommand(
            fallbackMethod = "timeoutFallback",
            commandProperties = {
               @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value="3000")
            }
    )
    public String timeout2(Integer timeout) {
        return myService.retry(timeout);
    }

    public String timeoutFallback(Integer timeout) {
        return "success";
    }

//    @GetMapping("/cache")
//    public Friend cache(String name) {
//        @Cleanup HystrixRequestContext context =
//                HystrixRequestContext.initializeContext();
//
//        Friend friend = requestCacheService.requestCache(name);
//        friend = requestCacheService.requestCache(name);
//        return friend;
//    }

}
